/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.entidade.protocolo.Protocolo;
import br.com.deployment.entidade.tramite.Tramite;
import br.com.deployment.message.Msg;
import br.com.deployment.servico.SecaoServico;
import br.com.deployment.servico.TramiteServico;
import br.com.deployment.servico.UsuarioServico;
import br.com.deployment.util.TramiteModel;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerReceberProtocolo implements Serializable {

    @EJB
    TramiteServico tramiteServico;
    @EJB
    UsuarioServico usuarioServico;
    @EJB
    SecaoServico secaoServico;
    private List<Tramite> lstTramite;
    private Tramite[] tramites;
    private TramiteModel tramiteModel;
    private Tramite tramite;
    private String cod;

    @PostConstruct
    public void init() {
        tramite = new Tramite();
        initSearch();
    }

    public void initSearch() {
        lstTramite = tramiteServico.findMyProtocolo(usuarioServico.getUsuario(), null);
        tramiteModel = new TramiteModel(lstTramite);
    }

    public String getCod() {
        return cod;
    }

    public void setCod(String cod) {
        this.cod = cod;
    }

    public Tramite getTramite() {
        return tramite;
    }

    public void setTramite(Tramite tramite) {
        this.tramite = tramite;
    }

    public Tramite[] getTramites() {
        return tramites;
    }

    public void setTramites(Tramite[] tramites) {
        this.tramites = tramites;
    }

    public List<Tramite> getLstTramite() {
        return lstTramite;
    }

    public void setLstTramite(List<Tramite> lstTramite) {
        this.lstTramite = lstTramite;
    }

    public TramiteModel getTramiteModel() {
        return tramiteModel;
    }

    public void setTramiteModel(TramiteModel tramiteModel) {
        this.tramiteModel = tramiteModel;
    }

    public void qtdSelecionado() {
        System.err.print("Qtd:" + tramites.length);
    }

    public void protocoloFind() {
        Protocolo p = new Protocolo();
        p.setId(Integer.parseInt(cod));
        tramiteModel = new TramiteModel(tramiteServico.findMyProtocolo(
                usuarioServico.getUsuario(), p));

    }

    public void receber() {
        System.err.println("Recebendo");
        if (tramites.length > 0) {
            for (Tramite tr : tramites) {
                tr.setDataRecebe(new Date());
                tr.setUsrRecebe(usuarioServico.getUsuario());
                tr.setStatus(true);
                tramiteServico.update(tr);
            }
            initSearch();
            Msg.messagemInfo("Protocolo" + (tramites.length > 1 ? "s" : "") + " recebido" + (tramites.length > 1 ? "s" : ""));
        }
        Msg.messagemError("Nenhum protocolo selecionado");
    }
}
